window.workspace = function(){
	var response = <?#$respons?>;
	var container, monitor, room_items, store, grid, sm, playwin, playform, warnwin, warnform;
	var sid = {'query':Ext.id()};
	/***************************************************************
	 * start flash video
	 */
	var prefix   = 'MFFLVIDS', mview = {}; 
	var swfver   = "11.1.0";
    var xiswfurl = "";
    var params   = {quality:"high", allowscriptaccess:"always", allowfullscreen:"true",wmode:"opaque"};
    var swfargs  = {quality:"high", allowscriptaccess:"always", allowfullscreen:"true",wmode:"transparent"};
    window.imNotify = {};
	var loadJs = function(path) {
		var doc = document, e;
		e = doc.createElement('script');
		e.src = path;
		doc.head.appendChild(e);
	}
	var render = function(anchorid, studioid) {
		var swf= prefix+studioid;
	    var attributes = {id:swf, name:swf};
	    studioid = parseInt(studioid);
	    anchorid = parseInt(anchorid);
	    imNotify[studioid] = new flashHandler(studioid, anchorid, prefix);
	    var flashvars  = {"anchorid":anchorid,"baseuri":response.config.website,
	    	"flash2js":'imNotify['+studioid+'].flash2js',"studioid":studioid,"lang":"en"};
	    swfobject.embedSWF("/static/flash/liveplayer/LiveRoomPlayer.swf", swf, "420", "315",
	    		swfver, xiswfurl, flashvars, params, attributes);
	}
	var loadMonitor = function() {
		Ext.Ajax.request({
			url		: '/admin/live/monitor/video/replay.html',
			success : function(resp, req){
				var data = Ext.decode(resp.responseText);
				if(data.list.length > 0) {
					monitor.removeAll();
					for(var i = 0; i < data.list.length; i++) {
						var item = data.list[i];
						create_video(item.id, item.userid, item.author, item.title, item.link, item.starlv, item.warn);
					}
					monitor.doLayout(false,  true);
				}
			}
		});
	};
	
	//在线成员信息的获取
	function member(sid, title) {
		if(!mview[sid]) {
			mview[sid]       = {};//初始化一个对象
			mview[sid].user  = Ext.id();
			mview[sid].sm    = new Ext.grid.CheckboxSelectionModel();
			mview[sid].store = new Ext.data.JsonStore({
			    autoDestroy	: true,
			    url		    : '/admin/live/monitor/video/viewer.html',
			    remoteSort	: true,
			    sortInfo	: {
			    	field		: 'userid',
			        direction	: 'DESC'
				},
				baseParams	: {
					s       : imNotify[sid].members.join(','),
					start	: 0,
					limit	: 15
				},
			    root			: 'dataset',
			 	totalProperty	: 'total',
			    fields			: ['userid', 'nickname', 'thumb', 'lv', 'exp', 'email', 'emailverify',
			       'regtime', 'lasttime', 'lastip']
			});
			mview[sid].win = new Ext.Window({
				title	: title+'[IM-Members]',
				iconCls	: 'icon-user',	
				width	: 760,
				items	: [{
					xtype		: 'grid',
				    store		: mview[sid].store,
				    autoHeight  : false,
					height  	: 560,
				    columns		: [
				        mview[sid].sm,
						{header: 'UserID', width:60, sortable:true, dataIndex:'userid'},
						{header: 'UserName', width:80, dataIndex:'nickname'},
						{header: 'Email', width:120, dataIndex:'email'},
						{header: 'Lv', width:50, dataIndex:'lv'},
						{header: 'RegTime', width:100, dataIndex:'regtime'},
						{header: 'Avatar', width:60, sortable:true, dataIndex:'thumb', renderer:function(v){
							return '<img src="'+Ext.STATIC_URL+v+'" width="24" height="24" />';
						}},
						{header: 'LoginTime', width:100, dataIndex:'lasttime'},
						{header: 'LoginIp',width:80, dataIndex:'lastip'}
					],
				    viewConfig	: {
				        forceFit	 : true,
				        scrollOffset : 0
				    },
				    sm			: mview[sid].sm,
				    stripeRows	: true,
				    tbar		: [{
	    				text	: 'Reload',
	    				sid     : sid,
	    				iconCls	: 'icon-refresh',
	    				handler : function(obj){
	    					var sid = obj.initialConfig.sid;
	    					mview[sid].store.baseParams.s = imNotify[sid].members.join(',');
	    					mview[sid].store.reload(); 
	    					}
	    				},'-',{
	    					text	: 'KickUser',
	    					sid     : sid,
	    					iconCls	: 'icon-delete',
	    					handler : function(obj){
	    						var sid = obj.initialConfig.sid;
	    						var rcs = mview[sid].sm.getSelections();
	    						Ext.each(rcs, function(item){
	    							imNotify[sid].kickUser(item.get('userid'));
	    						});
	    					}
	    				},'-',{
	    					text	: 'GagUser',
	    					iconCls	: 'icon-edit',
	    					sid     : sid,
	    					handler : function(obj){
	    						var sid = obj.initialConfig.sid;
	    						var rcs = mview[sid].sm.getSelections();
	    						Ext.each(rcs, function(item){
	    							imNotify[sid].gagUser(item.get('userid'), 0);
	    						});
	    					}
	    				},'-','<b>Search For：</b>','UserName:',{
	    					xtype       : 'textfield',
							id	    	: mview[sid].user,
							width	    : 100
	    				},{
						text			: 'Do Search',
						iconCls			: 'icon-search',
						sid    		    : sid,
						handler: function(obj) {
							var sid = obj.initialConfig.sid;
							mview[sid].store.baseParams.user   = Ext.getCmp(mview[sid].user).getValue();
							mview[sid].store.reload(); 
						}
					}],
					bbar		: new Ext.PagingToolbar({
						pageSize	: 15,
						store		: mview[sid].store,
						displayInfo	: true
					})
				}],
				closeAction : 'hide'
			});
		}
		mview[sid].store.reload();
		mview[sid].win.show();
	}
	
	var win, form;
	function addForm(){
		if(!win) {
		    form = new Ext.form.FormPanel({
				frame		: true,
				border		: false,
				bodyStyle	: 'padding:8px;',
		        labelWidth	: 100,
		        labelAlign  : 'right',
		    	defaults	: {
		    		xtype	  : 'textfield'
		    	},
		    	items		: [{
					xtype		: 'hidden',
					name		: 's',
					value		: 0
				},{
					xtype		: 'numberfield',
					fieldLabel	: 'Increase Robots',
					width		: 120,
					name		: 'count',
					value       : '10',
					allowBlank	: false
				},{
					xtype		: 'numberfield',
					fieldLabel	: 'time(unit:minute)',
					width		: 120,
					name		: 'time',
					value       : '3',
					allowBlank	: false
				}]
			});
			win = new Ext.Window({
				title	: 'Add Robots',
				iconCls	: 'icon-edit',	
				width	: 280,
				items	: [form],
				closeAction : 'hide',
				bbar 	: {
					xtype		: 'toolbar',
					buttonAlign	: 'right',
					items		: [{
						text:'Submit', 
						iconCls:'icon-save',
						handler:function(){
						if(!form.getForm().isValid())
		        			return;
		        		form.getForm().submit({
			        		waitMsg 	: 'data submit processing...',
							waitTitle 	: 'Tips',
			        		clientValidation: true,
			        		url		: '/admin/live/monitor/video/addrobots.html',
				        	success	: function(form, action) {
				        		win.hide();
							},
							failure	: main.submitFailure
				    	});
					}}]
				}
			});
		}
		win.show();
	};
	
	function playForm(ids){
		if(!playwin) {
			playform = new Ext.form.FormPanel({
				frame		: true,
				border		: false,
				bodyStyle	: 'padding:8px;',
				labelWidth	: 120,
				fileUpload  : true,
				labelAlign  : 'right',
				items		: [{
					xtype		: 'hidden',
					name		: 'ids',
					value		: 0
				},{
					xtype		: 'textfield',
					inputType 	: 'file', 
			 		fieldLabel	: 'Filedata',
					name		: 'Filedata'
				},{
		    		xtype		: 'combo',
		            store		: new Ext.data.ArrayStore({
			        	fields: ['id','name'],
			        	data: main.cv2Array(response.duration)
			    	}),
					displayField	: 'name',
					valueField		: 'id',
					mode			: 'local',
					emptyText		: 'selected...',
		            fieldLabel		: 'Duration',
		            width			: 120,
		            editable		: false,
					triggerAction	: 'all',
					hiddenName		: 'duration',
		            name			: 'duration'
		        },{
		    		xtype		: 'combo',
		            store		: new Ext.data.ArrayStore({
			        	fields: ['id','name'],
			        	data: main.cv2Array(response.reason)
			    	}),
					displayField	: 'name',
					valueField		: 'id',
					mode			: 'local',
					emptyText		: 'selected...',
		            fieldLabel		: 'Reason',
		            width			: 280,
		            allowBlank		: false,
					triggerAction	: 'all',
					hiddenName		: 'remark',
		            name			: 'remark'
		        }]
			});
			playwin = new Ext.Window({
				title	: 'Disabled Anchor',
				iconCls	: 'icon-edit',	
				width	: 520,
				items	: [playform],
				closeAction : 'hide',
				url		: '/admin/live/basic/studio/dostatus.html',
				bbar 	: {
					xtype		: 'toolbar',
					buttonAlign	: 'right',
					items		: [{
						text:'Save', 
						iconCls:'icon-save',
						id      : (response.dosave_monitorplay_id = Ext.id()),
						handler:function(){
						if(!playform.getForm().isValid())
							return;
						playform.getForm().submit({
							waitMsg 	: 'data submit processing...',
							waitTitle 	: 'Tips',
							clientValidation: true,
							url		: '/admin/live/basic/studio/dostatus.html',
							success	: function(form, action) {
								store.reload();
								playwin.hide();
							},
							failure	: main.submitFailure
						});
					}}]
				}
			});
		}
		playwin.show();
		playform.getForm().reset();
		playform.getForm().findField('ids').setValue(ids);
	};
	
	function warnForm(ids){
		if(!warnwin) {
			warnform = new Ext.form.FormPanel({
				frame		: true,
				border		: false,
				bodyStyle	: 'padding:8px;',
				labelWidth	: 60,
				fileUpload  : true,
				labelAlign  : 'right',
				items		: [{
					xtype		: 'hidden',
					name		: 'ids',
					value		: 0
				},{
					xtype		: 'textarea',
					fieldLabel	: 'say',
					width		: 260,
					name		: 'say',
				}]
			});
			warnwin = new Ext.Window({
				title	: 'Warn Anchor',
				iconCls	: 'icon-edit',	
				width	: 400,
				items	: [warnform],
				closeAction : 'hide',
				url		: '/admin/live/monitor/video/dowarn.html',
				bbar 	: {
					xtype		: 'toolbar',
					buttonAlign	: 'right',
					items		: [{
						text:'Send', 
						iconCls:'icon-save',
						handler:function(){
						if(!warnform.getForm().isValid())
							return;
						warnform.getForm().submit({
							waitMsg 	: 'data submit processing...',
							waitTitle 	: 'Tips',
							clientValidation: true,
							url		: '/admin/live/monitor/video/dowarn.html',
							success	: function(form, action) {
								store.reload();
								warnwin.hide();
							},
							failure	: main.submitFailure
						});
					}}]
				}
			});
		}
		warnwin.show();
		warnform.getForm().reset();
		warnform.getForm().findField('ids').setValue(ids);
	};
	
	function create_video(id, userid, author, title, link, starlv, warn) {
		var obj = {
			id          : 'items-'+id,
			columnWidth	: .33,
			style		: 'padding:8px 0 8px 8px',
			items 		: [{
				title 		: title+'['+author+']'+'<font color="red">['+starlv+']</font>',
				titleCollapse: true,
				height      : 460,
				layout		: 'border',
				tbar		: [{
					text	: 'GoLive',
					iconCls	: 'icon-add',
					linkaddr: link,
					handler : function(obj){
						window.open(obj.initialConfig.linkaddr);
					}
			    },'-',{
			    	text	: 'Disabled',
			    	iconCls	: 'icon-delete',
			    	studioid: id,
			    	handler : function(obj){
			    		playForm(obj.initialConfig.studioid);
			    	}
			    },'-',{
			    	text	: 'Member',
			    	iconCls	: 'icon-edit',
			    	studioid: id,
			    	stitle  : title+'['+author+']',
					handler : function(obj){
						member(obj.initialConfig.studioid, obj.initialConfig.stitle);
					}
			    },'-',{
			    	text	: 'AddRobot',
			    	iconCls	: 'icon-user',
			    	studioid: id,
					handler : function(obj){
						addForm();
						form.getForm().findField('s').setValue(obj.initialConfig.studioid);
						form.getForm().findField('count').setValue(10);
					}
			    },'-',{
					text	: 'ResetRobot',
					iconCls	: 'icon-key',
					studioid: id,
					handler : function(obj){
						Ext.Ajax.request({
							url		: '/admin/live/monitor/video/reset.html',
							params	: {'ids':obj.initialConfig.studioid},
							success : function(resp, req){
								var data = Ext.decode(resp.responseText);
								if(data.code == 0) {
									Ext.Msg.alert('Tips', 'Reset Success.');
								} else {
									Ext.Msg.alert('Tips', data.msg);
								}
							}
						});
					}
				},'-',{
					text	: 'Close',
					iconCls	: 'icon-eye',
					studioid: id,
					handler : function(obj){
						monitor.remove('items-'+obj.initialConfig.studioid);
						monitor.doLayout(false,  true);
					}
				},'-',{
			    	text	: 'Warn'+'('+warn+')',
			    	iconCls	: 'icon-edit',
			    	studioid: id,
			    	handler : function(obj){
			    		warnForm(obj.initialConfig.studioid);
			    	}
			    }],
			    items		: [{
			    	region		: 'center',
			    	autoScroll  : true,
			    	anchorid    : userid,
			    	studioid    : id,
			    	html		: '<div id="'+prefix+id+'"></div>',
			    	listeners   : {
			    		'afterrender':function(obj){
			    			var st = obj.initialConfig;
			    			setTimeout(function(){
			    				render(st.anchorid, st.studioid);
			    			}, 1000);
			    		}
			    	}
			    },{
			    	region		: 'east',
			    	width       : 192,
			    	split       : true,
			    	collapseMode: 'mini',
			    	bodyBorder 	: false,
			    	animCollapse: true,
			    	minWidth    : 168,
			    	maxWidth    : 250,
			    	autoScroll  : true,
			    	html		: '<ul id="Chats'+id+'" class="chats"></ul>'
			    },{
			    	region			: 'south',
			    	height          : 80,
			    	layout          : 'column',
			    	title			: 'Chats',
			    	items			: [{
			    		id          : 'Msg'+id,
				    	xtype		: 'textarea',
						height		: 52,
						width		: 480,
						name		: 'msg'
					},{
						xtype		: 'button',
						style		: 'padding:30px 4px',
						text 		: 'Send',
						studioid    : id,
						handler		: function(obj){
							var st = obj.initialConfig;
							var msg= Ext.getCmp('Msg'+st.studioid).getValue();
							if(msg.trim().length > 0) {
								Ext.getCmp('Msg'+st.studioid).setValue('');
								imNotify[st.studioid] && imNotify[st.studioid].sendChat(msg, 0);
							}
						}
					}]
			    }]
			}]
		};
		monitor.insert(0, obj);
	}
	
	/*******************************************************************************************
	 * end flash video
	 */
	loadJs('/static/plugins/swfobject.js');
	loadJs('/static/plugins/chats.js');
	sm    = new Ext.grid.CheckboxSelectionModel();
	store = new Ext.data.JsonStore({
	    autoDestroy	: true,
	    url		    : '/admin/live/basic/studio/dolist.html',
	    remoteSort	: true,
	    sortInfo	: {
            field		: 'id',
            direction	: 'DESC'
		},
		baseParams	: {
			start	: 0,
			olstatus: 1,
			limit	: 20
		},
		autoLoad		: true,
	    root			: 'dataset',
	 	totalProperty	: 'total',
	    fields			: ['id', 'title', 'author', 'studiono', 'userid', 'thumb', 'olstatus', 'link', 
	       'neartime', 'lasttime', 'isrecommend', 'viewer', 'star', 'oltools', 'push', 'record', 'warn']
	});
	window.setInterval(function(){
		store.reload();//1分钟刷新一次
    }, 60000);
 	grid = {
		xtype		: 'grid',
	    store		: store,
	    height		: 256,
	    split       : true,
		collapseMode: 'mini',
		bodyBorder  : false,
		animCollapse: true,
		autoScroll  : true,
		region		: 'north', 
	    columns		: [
	        sm,
			{header: 'ID', width:60, sortable:true, dataIndex:'id'},
			{header: 'Title', width:120, dataIndex:'title', renderer:function(v,meta,obj){
				return '<a href="'+obj.get('link')+'" target="_blank">'+v+'</a>';
			}},
			{header: 'Anchor', width:80, dataIndex:'author'},
			{header: 'Viewer', width:40, sortable:true, dataIndex:'viewer'},
			{header: 'LiveTools', width:40, dataIndex:'oltools'},
			{header: 'NearTime', width:80, sortable:true, dataIndex:'neartime'},
			{header: 'StarLv', width:50, sortable:true, dataIndex:'star'},
			{header: 'Warn', width:50, sortable:true, dataIndex:'warn'},
			{header: 'PushUrl', width:160, dataIndex:'push'},
			{header: 'Record', width:50, dataIndex:'record', renderer:function(v,meta,obj){
				return '<a href="'+obj.get('record')+'" target="_blank">log</a>';
			}},
		],
	    viewConfig	: {
	        forceFit	 : true,
	        scrollOffset : 0
	    },
		listeners	: {
			'destroy':function(){
    			win && win.close();
    		},'rowdblclick':function(grid, index, e){
		    	var data = grid.store.getAt(index).copy();
		    	var idstr= prefix+data.get('id');
		    	if(!Ext.get(idstr)) {
		    		create_video(data.get('id'), data.get('userid'), data.get('author'), data.get('title'), data.get('link'), data.get('star'), data.get('warn'));
		    		monitor.doLayout(false,  true);
		    	}
		    },
		    'render':function(obj){
				main.gridRender(obj, ['thumb']);
	    	}
		},	
	    sm			: sm,
	    stripeRows	: true,
	    tbar		: [{
			text	: 'Reload',
			iconCls	: 'icon-refresh',
			handler : function(){
				store.reload(); 
			}
	    },'-',{
			text	: 'Disabled',
			iconCls	: 'icon-pause',
			handler : function(){
				var rcs = grid.sm.getSelections();
				if(Ext.isEmpty(rcs)) {
					Ext.Msg.alert('Tips', 'plz selected records.');
					return;
				}
				var ids= [];
				Ext.each(rcs, function(item){
					ids.push(item.get('id'));
				});
				playForm(ids);
			}
		},'-',{
			text	: 'ResetRobots',
			iconCls	: 'icon-key',
			handler : function(){
				var rcs = grid.sm.getSelections();
				if(Ext.isEmpty(rcs)) {
					Ext.Msg.alert('Tips', 'plz selected records.');
					return;
				}
				var ids= [];
				Ext.each(rcs, function(item){
					ids.push(item.get('id'));
				});
				Ext.Ajax.request({
					url		: '/admin/live/monitor/video/reset.html',
					params	: {'ids':ids.join(',')},
					success : function(resp, req){
						var data = Ext.decode(resp.responseText);
						if(data.code == 0) {
							Ext.Msg.alert('Tips', 'Reset Success.');
						} else {
							Ext.Msg.alert('Tips', data.msg);
						}
					}
				});
			}
		},'-','<b>Search For：</b>','Title/Anchor:',{
			xtype       : 'textfield',
			id	    	: sid.query,
			width	    : 120
		},'-',{
			text			: 'DoSearch',
			iconCls			: 'icon-search',
			handler: function() {
				store.baseParams.query    = Ext.getCmp(sid.query).getValue();
				store.reload(); 
			}
		}],
		bbar		: new Ext.PagingToolbar({
			pageSize	: main.pageSize,
			store		: store,
			displayInfo	: true
		})
	};
 	monitor   = new Ext.ux.Portal({
		tbar		: [{
			text	: 'ReFlushStudio',
			iconCls	: 'icon-refresh',
			handler : function(){
				loadMonitor();
			}
	    }],
		xtype		: 'portal',
		region		: 'center',
		autoScroll 	: true,
		items		: []
	});
 	
 	container = {
		title		: response.title,
		layout		: 'border',
		region		: 'center',
		listeners	: {
		    'render':function(obj){
		    	loadMonitor();
	    	}
		},	
		items		: [grid, monitor]
	};
	return container;
}();